<?php

/**
 * Admin_Monitor_Memory Model
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Monitor_Memory_Models
 * @package  Admin_Monitor_Memory_Package
 * @author   Johari <joharijumali@gmail.com>
 */

namespace Admin\Models\Monitor;

use \Laravel\Database\Eloquent\Model as Eloquent;

/**
 * Class Admin_Monitor_Memory Model
 * 
 * Class Description
 *
 * @category Admin_Monitor_Memory_Models
 * @package  Admin_Monitor_Memory_Package
 * @author   Johari <joharijumali@gmail.com>
 */

class Memory extends Eloquent 
{

	public static $timestamps = true;
    public static $table = 'server_mem';
    public static $key = 'mem_id';

	/**
     * Function cron that pass parameter
     *
     * @param integer $id   the integer to quote
     * @param varchar $data the varchar to quote
     * @param varchar $date the varchar to quote
	 *
	 * @return parameter.
     */
    public static function cron($id, $data, $date)
	{	
        $data = json_decode($data);

        $exist = Memory::where('server_id', '=', $id)->first(array('mem_id'));

        $Memory = (empty($exist))? new Memory : Memory::find($exist->mem_id);

        $Memory->server_id = $id;
        $Memory->mem_memused = $data->memused;
        $Memory->mem_kbmemfree = $data->kbmemfree;
        $Memory->mem_kbmemused = $data->kbmemused;
        $Memory->mem_kbbuffers = $data->kbbuffers;
        $Memory->mem_kbcached = $data->kbcached;
        $Memory->mem_kbswpfree = $data->kbswpfree;
        $Memory->mem_kbswpused = $data->kbswpused;
        $Memory->mem_swpused = $data->swpused;
        $Memory->mem_kbswpcad = $data->kbswpcad;
        $Memory->mem_date = $date;
        $Memory->save();

        Memory::_stor($id, $data, $date, 'stor_mem');
    }
	
	/**
     * Function stor that pass parameter
     *
     * @param integer $id    the integer to quote
     * @param varchar $data  the varchar to quote
     * @param varchar $date  the varchar to quote
     * @param varchar $table the varchar to quote
	 *
	 * @return parameter.
     */
    protected static function _stor($id, $data, $date, $table)
	{
        static::$table = $table;

        $Memory = new Memory;

        $Memory->server_id = $id;
        $Memory->mem_memused = $data->memused;
        $Memory->mem_kbmemfree = $data->kbmemfree;
        $Memory->mem_kbmemused = $data->kbmemused;
        $Memory->mem_kbbuffers = $data->kbbuffers;
        $Memory->mem_kbcached = $data->kbcached;
        $Memory->mem_kbswpfree = $data->kbswpfree;
        $Memory->mem_kbswpused = $data->kbswpused;
        $Memory->mem_swpused = $data->swpused;
        $Memory->mem_kbswpcad = $data->kbswpcad;
        $Memory->mem_date = $date;
        $Memory->save();
    }

    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabSingleMemory($server_id)
    {
        
        $Memory = Memory::where('server_id', '=', $server_id)->first()->to_array();

        return $Memory;
    }


    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabAllMemory()
    {
        
        $Memory = Memory::all();

        $data = array();

        foreach ($Memory as $key => $value) {
            $data[$value->server_id] = $value->to_array();

            unset($data[$value->server_id]['created_at']);
            unset($data[$value->server_id]['updated_at']);
            unset($data[$value->server_id]['mem_date']);
        }



        return $data;
    }


}
